﻿Imports MySql.Data.MySqlClient
Imports System.IO
Public Class frmItemType
    Public NewEntry_State As Boolean
    Dim Strsql As String
    Dim mysql As New ClsSQLhelper
    Dim On_load As Boolean = True
    Dim DA As New MySqlDataAdapter
    Dim DAUnit As New MySqlDataAdapter
    Dim BDS As New BindingSource
    Dim BDSUnit As New BindingSource
    Dim commandBuilder As New MySqlCommandBuilder
    Public Primary_Key As Integer
    Dim DTSection As New DataTable
#Region "Function"

    Function Validate_Data() As Boolean
        Validate_Data = False
        If Me.txtitemname.Text = "" Then
            MsgBox("กรุณาใส่ชื่อวัสดุ", MsgBoxStyle.Exclamation)
            Exit Function
        End If



        If Me.txtuom.Text = "" Then
            MsgBox("กรุณาใส่หน่วยนับ", MsgBoxStyle.Exclamation)
            Exit Function
        End If

        'If Me.Gridcontainunit.RowCount <= 1 Then
        '    MsgBox("กรุณาใส่หน่วยบรรจุอย่างน้อย 1 ตัว", MsgBoxStyle.Exclamation)
        'End If

        'If Me.GridStock.RowCount <= 1 Then
        '    MsgBox("กรุณาเลือกคลังจัดเก็บอย่างน้อย 1 คลัง", MsgBoxStyle.Exclamation)
        'End If
        Validate_Data = True
        Return Validate_Data

    End Function
    Sub Load_Site()

        Dim Col_Site As New DataGridViewComboBoxColumn
        Dim Col_Item As New DataGridViewTextBoxColumn
        Dim Col_MinStockQty As New DataGridViewTextBoxColumn
        Dim Col_MaxStockQty As New DataGridViewTextBoxColumn
        Dim Col_MinReOrderQty As New DataGridViewTextBoxColumn
        Dim Col_MaxReOrderQty As New DataGridViewTextBoxColumn
        Dim Col_MinThermo As New DataGridViewTextBoxColumn
        Dim Col_MaxThermo As New DataGridViewTextBoxColumn

        With Col_Site
            .HeaderText = "คลัง"
            .Width = 200
            Strsql = "select siteid,site from site  order by site"
            Dim DTSite As New DataTable
            DTSite = mysql.GetMYSQLDataTable(Strsql, "site")
            .DataSource = DTSite
            .DisplayMember = "site"
            .ValueMember = "siteid"
            .DataPropertyName = "site"
        End With

        With Col_Item
            .Visible = False
            .DataPropertyName = "itemnumber"

        End With


        With Col_MinStockQty
            .HeaderText = "จำนวนต่ำสุด"
            .Width = 100
            .DataPropertyName = "minstockquantity"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        End With

        With Col_MaxStockQty
            .HeaderText = "จำนวนมากสุด"
            .Width = 100
            .DataPropertyName = "maxstockquantity"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_MinReOrderQty
            .HeaderText = "ซื้ออย่างน้อย"
            .Width = 100
            .DataPropertyName = "reordermaxquantity"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_MaxReOrderQty
            .HeaderText = "ซื้อได้ไม่เกิน"
            .Width = 100
            .DataPropertyName = "reorderminquantity"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_MinThermo
            .HeaderText = "อุณหภูมิต่ำสุด"
            .Width = 100
            .DataPropertyName = "thermo_low"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        With Col_MaxThermo
            .HeaderText = "อุณหภูมิสูงสุด"
            .Width = 100
            .DataPropertyName = "thermo_hight"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        'With GridStock
        '    '.Columns.Add(Col_SiteID)
        '    Strsql = "Select * from siteitem where itemnumber = " & Me.TxtItemNumber.Text
        '    Dim DTStock As New DataTable
        '    DA = New MySqlDataAdapter(Strsql, ClsSQLhelper.MySQLConnectionStr)
        '    DTStock = DSWHIS.Tables("siteitem")
        '    DA.Fill(DTStock)
        '    BDS.DataSource = DTStock
        '    commandBuilder = New MySqlCommandBuilder(DA)
        '    .AutoGenerateColumns = False
        '    .DataSource = BDS
        '    .Columns.Add(Col_Site)
        '    .Columns.Add(Col_Item)
        '    .Columns.Add(Col_MinStockQty)
        '    .Columns.Add(Col_MaxStockQty)
        '    .Columns.Add(Col_MinReOrderQty)
        '    .Columns.Add(Col_MaxReOrderQty)
        '    .Columns.Add(Col_MinThermo)
        '    .Columns.Add(Col_MaxThermo)
        'End With
    End Sub
    Sub Load_itemcontainunit()
        Try
            Dim Col_Item As New DataGridViewTextBoxColumn
            Dim Col_containunit As New DataGridViewTextBoxColumn
            Dim Col_qtyperunit As New DataGridViewTextBoxColumn
            Dim Col_note As New DataGridViewTextBoxColumn
            Dim Col_active As New DataGridViewCheckBoxColumn
            Dim Col_StandardPrice As New DataGridViewTextBoxColumn

            With Col_Item
                .Visible = False
                .DataPropertyName = "itemid"
            End With

            With Col_containunit
                .HeaderText = "หน่วยบรรจุ"
                .Width = 100
                .DataPropertyName = "containunit"
            End With

            With Col_qtyperunit
                .HeaderText = "จำนวนบรรจุ"
                .Width = 100
                .DataPropertyName = "qtyperunit"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With

            With Col_StandardPrice
                .HeaderText = "ราคากลาง"
                .Width = 100
                .DataPropertyName = "standardprice"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With

            With Col_active
                .HeaderText = "เปิดใช้งาน"
                '.Width = 100
                .DataPropertyName = "active"

            End With

            With Col_note
                .HeaderText = "note"
                .Width = 100
                .DataPropertyName = "หมายเหตุ"
            End With


            'Strsql = "Select itemid,containunit,qtyperunit,standardprice,note,active from itemcontainunit where itemid = " & Me.TxtItemNumber.Text
            'Dim DTcontainunit As New DataTable
            'DAUnit = New MySqlDataAdapter(Strsql, ClsSQLhelper.MySQLConnectionStr)
            'DTcontainunit = DSWHIS.Tables("itemcontainunit")
            'DAUnit.Fill(DTcontainunit)
            'BDSUnit.DataSource = DTcontainunit
            'commandBuilder = New MySqlCommandBuilder(DAUnit)

            'With Gridcontainunit
            '    .AutoGenerateColumns = False
            '    .DataSource = BDSUnit
            '    .Columns.Add(Col_Item)
            '    .Columns.Add(Col_containunit)
            '    .Columns.Add(Col_qtyperunit)
            '    .Columns.Add(Col_StandardPrice)
            '    .Columns.Add(Col_active)
            '    .Columns.Add(Col_note)
            'End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_itemcontainunit"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = ""
                .ShowDialog()
            End With
        End Try


    End Sub
    Sub Load_ProductGroup()
        Try
            Strsql = "Select section_id,section_name from section order by readonly desc,section_name"

            DTSection = mysql.GetMYSQLDataTable(Strsql, "section")
            With Me.Cbosection
                .DataSource = DTSection
                .DisplayMember = "section_name"
                .ValueMember = "section_id"
            End With
            Cbosection.SelectedValue = My.Settings.Current_Item_Section
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ProductGroup"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub
    Sub Load_ProductType(Optional ByVal ProductGroup As Integer = 0)
        Try
            Strsql = "Select Category_id,Category_name from category where sectionid = " & ProductGroup & " order by readonly desc,Category_name"
            Dim DTcategory As New DataTable
            DTcategory = mysql.GetMYSQLDataTable(Strsql, "category")
            With Me.CboCategory
                .DataSource = DTcategory
                .DisplayMember = "Category_name"
                .ValueMember = "Category_id"
            End With
            CboCategory.SelectedValue = My.Settings.Current_Item_Category
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ProductType"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Load_ProductKind()
        Try
            Strsql = "Select kind_id,kind_name from item_kind order by kind_id"
            Dim DT_Kind As New DataTable
            DT_Kind = mysql.GetMYSQLDataTable(Strsql, "item_kind")
            With Me.CboKind
                .DataSource = DT_Kind
                .DisplayMember = "kind_name"
                .ValueMember = "kind_id"
            End With
            CboKind.SelectedValue = My.Settings.Current_Item_Kind
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ProductKind"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub
    Sub Load_ItemData()
        Try
            On_load = True
            Strsql = "Select * from itemtype where itemnumber = " & Primary_Key
            Dim DTItem As New DataTable
            DTItem = mysql.GetMYSQLDataTable(Strsql, "itemtype")
            Me.TxtItemNumber.Text = Primary_Key
            Me.txtitemname.Text = DTItem.Rows(0).Item("itemname").ToString


            Me.TxtImagepart.Text = DTItem.Rows(0).Item("itemimage").ToString

            If Me.TxtImagepart.Text <> "" Then
                If File.Exists(Me.TxtImagepart.Text) = True Then

                    Dim bm As New Bitmap(Me.TxtImagepart.Text)
                    PictureEdit1.Image = bm
                Else
                    MsgBox("ไม่พบรูปนี้จากในระบบ ไฟล์รูปภาพอาจถูกลบ หรือเปลี่ยนที่เก็บ", MsgBoxStyle.Critical, "ไม่สามารถแสดงรูปได้")
                End If

            End If

            Me.txtuom.Text = DTItem.Rows(0).Item("uom").ToString
            Me.txtstandNumber.Text = DTItem.Rows(0).Item("standnumber").ToString
            Me.txtLocalnumber.Text = DTItem.Rows(0).Item("localnumber").ToString
            Me.TxtBarcode.Text = DTItem.Rows(0).Item("alternatenumber").ToString
            Me.TxtDesc.Text = DTItem.Rows(0).Item("note").ToString
            Me.NUDStandardprice.Value = DTItem.Rows(0).Item("standdardprice")
            Me.NUDCostPrice.Value = DTItem.Rows(0).Item("cost")
            Me.Cbosection.SelectedValue = DTItem.Rows(0).Item("section")
            Load_ProductType(Me.Cbosection.SelectedValue)
            Me.CboCategory.SelectedValue = DTItem.Rows(0).Item("category")
            Me.CboKind.SelectedValue = DTItem.Rows(0).Item("kind")
            Me.ChkActive.Checked = DTItem.Rows(0).Item("active")
            If IsDBNull(DTItem.Rows(0).Item("registerdate")) = True Then
                Me.DTPDateRegister.Value = Now
            Else
                Me.DTPDateRegister.Value = DTItem.Rows(0).Item("registerdate")
            End If
            On_load = False
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ItemData"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub

    Sub Copy_Image2Server()
        Dim StrFilename As String = ""
        Try

            StrFilename = My.Settings.ImgSharePart & "\" & Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour & Now.Minute & Now.Second & ".jpg"

            File.Copy(Me.TxtImagepart.Text, StrFilename, True)
            Dim bm As New Bitmap(Me.TxtImagepart.Text)
            PictureEdit1.Image = bm
            Me.TxtImagepart.Text = StrFilename
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Copy_Image2Server"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = "StrFilename = " & StrFilename
                .ShowDialog()
            End With
        End Try

    End Sub
#End Region
    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Me.Dispose()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Validate_Data() = False Then Exit Sub
        If MsgBox("คุณต้องการบันทึกข้อมูลใช่หรือไม่ ?:", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยัน") = MsgBoxResult.No Then Exit Sub
        Select Case NewEntry_State
            Case True
                'Insert Table Item
                Strsql = "Insert into itemtype (registerdate,itemname,alternatenumber,section,category,kind,itemimage,standdardprice,cost,standnumber,localnumber,uom,note,active) "
                Strsql = Strsql & " values('" & Pn_Framework.MysqlDateTimeFormat(Me.DTPDateRegister.Value, False) & "','" & Me.txtitemname.Text & "','" & Me.TxtBarcode.Text & "'," & Me.Cbosection.SelectedValue & "," & Me.CboCategory.SelectedValue & " , " & CboKind.SelectedValue

                If Me.TxtImagepart.Text = "" Then
                    Strsql = Strsql & ",''"
                Else
                    Copy_Image2Server()
                    Strsql = Strsql & ",'" & Replace(Me.TxtImagepart.Text, "\", "\\") & "'"
                End If

                Strsql = Strsql & "," & Me.NUDStandardprice.Value & "," & Me.NUDCostPrice.Value & ",'" & Me.txtstandNumber.Text & "','" & Me.txtLocalnumber.Text
                Strsql = Strsql & "','" & Me.txtuom.Text & "','" & Me.TxtDesc.Text & "'," & Me.ChkActive.Checked & ")"
                mysql.MySQLExecute(Strsql)
                NewEntry_State = False
                Me.BtnDelete.Enabled = True

                Strsql = "Select itemnumber from itemtype order by itemnumber desc limit 1"
                Me.TxtItemNumber.Text = mysql.MySQLExecuteScalar(Strsql)

            Case False
                Strsql = "Update itemtype set registerdate='" & Pn_Framework.MysqlDateTimeFormat(Me.DTPDateRegister.Value, False) & "',itemname= '" & Me.txtitemname.Text & "',alternatenumber= '" & Me.TxtBarcode.Text & "',section=" & Me.Cbosection.SelectedValue & ",kind=" & Me.CboKind.SelectedValue
                Strsql = Strsql & ",category=" & Me.CboCategory.SelectedValue


                If Me.TxtImagepart.Text = "" Then
                    Strsql = Strsql & ",itemimage = ''"
                Else
                    Copy_Image2Server()
                    Strsql = Strsql & ",itemimage = '" & Replace(Me.TxtImagepart.Text, "\", "\\") & "'"
                End If

                Strsql = Strsql & ",standdardprice=" & Me.NUDStandardprice.Value & ",cost=" & Me.NUDCostPrice.Value
                Strsql = Strsql & ",standnumber= '" & Me.txtstandNumber.Text & "',localnumber= '" & Me.txtLocalnumber.Text & "',uom= '" & Me.txtuom.Text & "',note= '" & Me.TxtDesc.Text
                Strsql = Strsql & "',active=" & Me.ChkActive.Checked & " where itemnumber = " & Me.TxtItemNumber.Text
                mysql.MySQLExecute(Strsql)


        End Select
        MsgBox("บันทึกข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Dispose()



    End Sub

    Private Sub frmNewItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_ProductGroup()
        Load_ProductType(Cbosection.SelectedValue)
        Load_ProductKind()
        If Me.NewEntry_State = False Then
            Load_ItemData()
            Load_Site()
            Load_itemcontainunit()
            Me.BtnDelete.Enabled = True
        End If

        On_load = False
    End Sub

    Private Sub BtnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBrowse.Click
        Try
            With Me.OpenFileDialog1
                .Title = "เลือกรูปภาพ"
                .FileName = ""
                .Filter = "Jpeg Image|*.jpg"
                If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

                Me.TxtImagepart.Text = .FileName
                Dim bm As New Bitmap(Me.TxtImagepart.Text)
                PictureEdit1.Image = bm

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "PicLoadImg_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub Cbosection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbosection.SelectedIndexChanged
        If On_load = True Then Exit Sub
        Load_ProductType(Me.Cbosection.SelectedValue)
    End Sub

  
    Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
        Try
            If Me.TxtBarcode.Text.Trim.Length = 0 Then Exit Sub
            'BarCodeCtrl1.Print()
            Dim MyBarcode As New Cls_Barcode
            If DlgTotalPage.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
            MyBarcode.Print_Barcode(Me.TxtBarcode.Text, DlgTotalPage.NUDTotalPage.Value)
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub BtnGenBarcode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGenBarcode.Click

    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Try
            If txtuom.Text = "" Then Exit Sub

        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If MsgBox("คุณต้องการลบข้อมูลตัวนี้ใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub

        Strsql = "Delete from itemstype where itemnumber = " & Me.TxtItemNumber.Text
        mysql.MySQLExecute(Strsql)

        'Strsql = "Delete from siteitem where itemnumber = " & Me.TxtItemNumber.Text
        'mysql.MySQLExecute(Strsql)

        'Strsql = "Delete from itemcontainunit where itemid = " & Me.TxtItemNumber.Text
        'mysql.MySQLExecute(Strsql)

        MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Dispose()
    End Sub

    Private Sub CboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategory.SelectedIndexChanged

    End Sub
End Class